package servlet;

import dao.ReceiverDAOImpl;
import pojo.Receiver;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/receiver")
public class ReceiverServlet extends BaseServlet {

    // 添加接收方
    protected void addReceiver(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int receiverID = Integer.parseInt(req.getParameter("receiverID"));
        String receiverName = req.getParameter("receiverName");
        String receiverAddress = req.getParameter("receiverAddress");

        Receiver receiver = new Receiver(receiverID, receiverName, receiverAddress);
        ReceiverDAOImpl.add(receiver);
        sendJsonResponse(resp, new ApiResult(200, "Receiver added successfully.", null));
    }

    // 删除接收方
    protected void deleteReceiver(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int receiverID = Integer.parseInt(req.getParameter("receiverID"));
        ReceiverDAOImpl.delete(receiverID);
        sendJsonResponse(resp, new ApiResult(200, "Receiver deleted successfully.", null));
    }

    // 更新接收方
    protected void updateReceiver(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int receiverID = Integer.parseInt(req.getParameter("receiverID"));
        String receiverName = req.getParameter("receiverName");
        String receiverAddress = req.getParameter("receiverAddress");

        Receiver receiver = new Receiver(receiverID, receiverName, receiverAddress);
        ReceiverDAOImpl.update(receiver);
        sendJsonResponse(resp, new ApiResult(200, "Receiver updated successfully.", null));
    }

    // 获取所有接收方
    protected void getAllReceivers(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Receiver> receivers = ReceiverDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", receivers));
    }

    // 根据ID获取接收方
    protected void getReceiverById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int receiverID = Integer.parseInt(req.getParameter("receiverID"));
        Receiver receiver = ReceiverDAOImpl.getById(receiverID);
        sendJsonResponse(resp, new ApiResult(200, "Success", receiver));
    }

    // 获取接收方数量
    protected void getReceiverCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = ReceiverDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取接收方
    protected void getReceiversByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Receiver> receivers = ReceiverDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", receivers));
    }

    // 根据关键字搜索接收方
    protected void findReceiversByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Receiver> receivers = ReceiverDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", receivers));
    }

    // 根据关键字分页搜索接收方
    protected void findReceiversByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Receiver> receivers = ReceiverDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", receivers));
    }
}
